package org.edu.modules.kwgl.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.kwgl.entity.VeJwCjsetting;
import org.edu.modules.kwgl.service.IVeJwCjsettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 成绩类型
* @Author: jeecg-boot
* @Date:   2021-05-17
* @Version: V1.0
*/
@Api(tags="成绩录入设置")
@RestController
@RequestMapping("/kwgl/veJwCjsetting")
@Slf4j
public class VeJwCjsettingController extends BaseController<VeJwCjsetting, IVeJwCjsettingService> {
   @Autowired
   private IVeJwCjsettingService veJwCjsettingService;

   /**
    * 分页列表查询
    *
    * @param veJwCjlx
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "成绩录入设置-分页列表查询")
   @ApiOperation(value="成绩录入设置-分页列表查询", notes="成绩录入设置-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(VeJwCjsetting veJwCjlx,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<VeJwCjsetting> queryWrapper = QueryGenerator.initQueryWrapper(veJwCjlx, req.getParameterMap());
       Page<VeJwCjsetting> page = new Page<VeJwCjsetting>(pageNo, pageSize);
       IPage<VeJwCjsetting> pageList = veJwCjsettingService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @param veJwCjlx
    * @return
    */
   @AutoLog(value = "成绩录入设置-添加")
   @ApiOperation(value="成绩录入设置-添加", notes="成绩录入设置-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody VeJwCjsetting veJwCjlx) {
       veJwCjsettingService.save(veJwCjlx);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param veJwCjlx
    * @return
    */
   @AutoLog(value = "成绩录入设置-编辑")
   @ApiOperation(value="成绩录入设置-编辑", notes="成绩录入设置-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody VeJwCjsetting veJwCjsetting) {
       veJwCjsettingService.updateById(veJwCjsetting);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "成绩录入设置-通过id删除")
   @ApiOperation(value="成绩录入设置-通过id删除", notes="成绩录入设置-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       veJwCjsettingService.removeById(id);
       return Result.ok("删除成功!");
   }


    /**
     *  批量启用禁用
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "成绩录入设置-启用禁用")
    @ApiOperation(value="成绩录入设置-启用禁用", notes="成绩录入设置-启用禁用")
    @PutMapping(value = "/stateBatch")
    public Result<?> stateBatch(@RequestBody JSONObject jsonObject) {
        if(!jsonObject.containsKey("ids")){
            return Result.error("参数错误!");
        }
        if(!jsonObject.containsKey("state")){
            return Result.error("参数错误!");
        }
        String ids = jsonObject.getString("ids");
        Integer state = jsonObject.getInteger("state");

        VeJwCjsetting data = new VeJwCjsetting();
        data.setStatus(state);

        LambdaQueryWrapper<VeJwCjsetting> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(VeJwCjsetting::getId,Arrays.asList(ids.split(",")));
        veJwCjsettingService.update(data,queryWrapper);
        return Result.ok(state == 1 ? "启用成功!":"禁用成功");
    }


    /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "成绩录入设置-批量删除")
   @ApiOperation(value="成绩录入设置-批量删除", notes="成绩录入设置-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.veJwCjsettingService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "成绩录入设置-通过id查询")
   @ApiOperation(value="成绩录入设置-通过id查询", notes="成绩录入设置-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       VeJwCjsetting veJwCjsetting = veJwCjsettingService.getById(id);
       if(veJwCjsetting==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(veJwCjsetting);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param veJwCjsetting
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, VeJwCjsetting veJwCjsetting) {
       return super.exportXls(request, veJwCjsetting, VeJwCjsetting.class, "成绩录入设置");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, VeJwCjsetting.class);
   }

}
